﻿using System;
using System.Collections.Generic;
using System.Linq;
using NOSDC.Entities;
using NOSDC.Pocos;
using System.Data.Objects;

namespace NOSDC.Repositories
{
    public class SpeakerRepository : ISpeakerRepository
    {
       private readonly NOSDCContext _context;

       public SpeakerRepository()
        {
            _context = new NOSDCContext();
        }

        public IEnumerable<Speaker> GetAll()
        {
            return _context.Speaker.ToList();
        }

        public Speaker GetById(int id)
        {
            return _context.Speaker.FirstOrDefault(speaker => speaker.SpeakerId.Equals(id));
        }

        public int Add(Speaker entity)
        {
            _context.Speaker.AddObject(entity);
            return _context.SaveChanges();
        }

        public bool Delete(Speaker entity)
        {
            throw new NotImplementedException();
        }

        public int Update(Speaker entity)
        {
            _context.Speaker.Attach(entity);
            var speakerEntry = _context.ObjectStateManager.GetObjectStateEntry(entity);

            speakerEntry.SetModified();
            _context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            
            return _context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
        }
    }
}
